<template>
    <view class="content">
		<!-- <u-navbar :is-back="false" :border-bottom="false" title=" " :background="top_bg_color">
			<view class="title">
				<view>购物车</view>
				<view class="num">(共{{count}}件)</view>
			</view>
		</u-navbar> -->
		<view class="wrapper">
			<view class="item" v-for="(item, index) in 5" :key="index">
				<view class="item-cen">
					<image :src="item.checked == true ? '../../static/icon/choose_1.png' : '../../static/icon/choose_2.png'" 
						class="choose-1" @click="checked(item)"></image>
					<image src="../../static/del/17.png" class="img"></image>
					<view class="item-right">
						<view class="name">销售实战派-85节实战秘籍销售实战派-85节实战秘籍</view>
						<view class="sub">共86节</view>
						<view class="price">
							￥<text class="price-num">169.00</text>
						</view>
						<view class="gift">
							<view class="gift-icon">赠</view>
							<view class="gift-text">
								《商务情景话术内容展》《商务情景话术内容展》《销冠笔记》
							</view>
						</view>
					</view>
				</view>
				<view class="down">
					<u-number-box v-model="count" input-width="70" :disabled-input="true"
					    :index="index" :min="1" @change="valChange(index, $event)"></u-number-box>
				</view>
			</view>
		</view>
		<view class="none" v-if="list && list.length == 0" @click="$sTo('/pages/tabbar/index')">
		    <view>您的购物车暂无商品</view>
		    <view>去商城逛逛吧</view>
		</view>
		
		<view class="footer">
			<view class="all" @click="selectAll()">
				<image :src="checkAll == true ? '../../static/icon/choose_1.png' : '../../static/icon/choose_2.png'" class="choose-1"></image>
				<view>全选</view>
			</view>
			<view class="t-price">
				合计：￥<text class="t-price-num">{{ allMoney.toFixed(2) }}</text>
			</view>
			<view class="del" @click="del(item)">删除</view>
			<view class="f-btn" @click="subOrder">去结算</view>
		</view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                // top_bg_color: { backgroundColor: 'rgb(255, 255, 255)'},
				count: 36,
				checkAll: false, //全选
				list: [],
				allMoney: 0, //价钱
				isShow: false,
            };
        },
        onShow() {
            this.allMoney = 0
            this.checkAll = false
            if (this.$getSync('userToken')) {
                this.getList();
            }
        },
        methods: {
            //去结算
            subOrder() {
            	var idss = []
            	var goods = []
            	this.list.forEach((item, index) => {
            		if (item.checked == true) {
            			idss.push(item.id)
            			var obj = {
            				goods_id: item.goods_id, //商品id
            				goods_sku_id: item.goods_sku_id, //规格id
                            // goods_sku_name: item.goods_sku_name, //规格名称
                            goods_nums: item.goods_nums //商品数量
            			}
            			goods.push(obj)
            		}
            	})
            	if (idss == '') {
            		this.$toast("请选择商品后结算")
            		return
            	}
            	this.$ajax('orders/confirmOrder', {
            		userToken: this.$getSync('userToken'),
                    buyType: 1, 
            		buy_id: idss.join(','), //购物车id	
            		goods: goods, //[{"goods_id":1,"goods_sku_id":1,"goods_sku_name":'',"goods_nums":1}]
            	}).then(ret => {
            		if (ret.status == 0) {
            			this.$gTo('/pages/settlement/settlement?data=' + JSON.stringify(ret.data) + '&buyId=' + idss.join(','))
            		} else {
            			this.$toast(ret.message)
            		}
            	})
            },
            
            //单选
            checked(item) {
                let sellectList = [];
                item.checked = !item.checked;
                this.list.forEach((el, index) => {
                    if (el.checked) {
                        sellectList.push(el)
                    }
                })
                if (sellectList.length == this.list.length) {
                    this.checkAll = true
                } else {
                    this.checkAll = false
                }
                this.zongji()
            },
            
            
            //全选
            selectAll() {
                this.checkAll = !this.checkAll;
                if (this.checkAll) {
                    this.list.forEach((item, index) => {
                        item.checked = true
                    })
                } else {
                    this.list.forEach((item, index) => {
                        item.checked = false
                    })
                }
                this.zongji()
            },
            
            //修改数量
            valChange(index, e) {
            	this.list[index].goods_nums = e.value;
                this.$ajax('orders/editCart', {
                    userToken: this.$getSync('userToken'),
                    id: this.list[index].id, //购物车id
                    goods_nums: e.value,
                }).then(ret => {
                    if (ret.status == 0) {
                        this.zongji();
                    } else {
                        this.$toast(ret.message);
                    }
                });
            },
            
            //总计
            zongji() {
            	var a = false;
            	this.allMoney = 0
            	this.list.forEach(item => {
            		if (!item.checked) {
            			a = true
            		} else {
                        this.allMoney += item.goods_nums * item.sale_price
            		}
            	})
            	if (!a) {
            		this.checkAll = true
            	} else {
            		this.checkAll = false
            	}
            },
            
            //删除商品
            del(item) {
                var ids = [];
                this.list.forEach((el) => {
                    if (el.checked) {
                        ids.push(el.id)
                    }
                })
                if (ids.length == 0) {
                    this.$toast('请选择商品');
                    return false;
                }
                this.$confirm('确定删除商品？',()=>{
                    this.$ajax('delCart', {
                        userToken: this.$getSync('userToken'),
                        id: ids.join(','), //购物车id
                    }).then(ret => {
                        if (ret.status == 0) {
                            this.$toast('删除成功');
                            setTimeout((res) => {
                            	this.getList();
                                this.checkAll = false;
                                this.allMoney = 0;
                            }, 500)
                        } else {
                            this.$toast(ret.message);
                        }
                    });
                })
            },
            
            //获取购物车列表
            getList() {
                this.$ajax('orders/buycarList', {
                    userToken: this.$getSync('userToken'),
                }).then(ret => {
                    if (ret.status == 0) {
                        this.list = ret.data;
                        if (this.list.length > 0) {
                        	this.list.forEach(val => {
                        		this.$set(val, 'checked', false)
                        	})
                        }
                        // this.isShow = true;
                    } else {
                        this.$toast(ret.message);
                    }
                });
            },
            
        }
    };
</script>

<style lang="scss">
	.none{
	    display: flex;
	    flex-direction: column;
	    justify-content: center;
	    align-items: center;
	    font-size: 36rpx;
	    color: #bfbfbf;
	    margin: 130px auto 0;
	}
	
    page{
        background-color: #f5f5f5;
    }
	.title{
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 32rpx;
		font-weight: bold;
		position: relative;
		left: 50%;
	}
	.num{
		font-size: 28rpx;
		font-weight: normal;
	}
	.content{
		padding-bottom: 70px;
	}
	.wrapper{
		margin: 20rpx;
		padding: 15rpx;
		background-color: #fff;
		border-radius: 20rpx;
	}
	.item{
		padding: 40rpx 0;
		border-bottom: 2rpx solid #e6e6e6;
	}
	.item:last-child{
		border-bottom: none;
	}
	.item-cen{
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.choose-1{
		width: 31rpx;
		height: 31rpx;
		margin-right: 12rpx;
	}
	.img{
		width: 160rpx;
		height: 160rpx;
		border: 2rpx solid #e6e6e6;
		border-radius: 10rpx;
		margin: 0 18rpx 0 8rpx;
	}
	.item-right{
		width: 440rpx;
		flex: 1;
	}
	.name{
		font-size: 28rpx;
		font-weight: bold;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	.sub{
		font-size: 24rpx;
		color: #7d7d7d;
		padding-top: 5rpx;
	}
	.price{
		font-size: 22rpx;
		padding-top: 5rpx;
	}
	.price-num{
		font-size: 34rpx;
		font-weight: bold;
	}
	.gift{
		display: flex;
		justify-content: space-between;
		align-items: flex-start;
	}
	.gift-icon{
		width: 29rpx;
		height: 29rpx;
		font-size: 20rpx;
		color: #fff;
		text-align: center;
		line-height: 29rpx;
		background-color: #fe4a26;
		border-radius: 6rpx;
		margin-right: 4rpx;
	}
	.gift-text{
		flex: 1;
		font-size: 24rpx;
		color: #7d7d7d;
	}
	.down{
		display: flex;
		justify-content: flex-end;
		padding-top: 10rpx;
	}
	
	.footer{
		width: 100%;
		height: 100rpx;
		background-color: #fff;
		border-top: 2rpx solid #e6e6e6;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 20rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 999;
	}
	.all{
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 24rpx;
		color: #545454;
		padding-right: 25rpx;
	}
	.t-price{
		flex: 1;
		font-size: 24rpx;
		font-weight: bold;
	}
	.t-price-num{
		font-size: 36rpx;
	}
	.del{
		font-size: 26rpx;
		color: #f76d0a;
		padding: 0 25rpx;
	}
	.f-btn{
		font-size: 28rpx;
		color: #fff;
		padding: 16rpx 35rpx;
		background-color: #2558ab;
		border-radius: 50rpx;
	}
	

</style>
